<?php
include('connect_db.php');
/*if logged in, redirect to homepage */
if (!checkLoggedin())
{
	header('Location: login.php');
}

if(ISSET($_GET['id']))
{
	$house_id = $_GET['id'];
}
else
{
	header('Location: index.php');
}

function create_image($width){
	if ($width > 1000)
		$width = 1000;

	$width = $width * 0.62;
	$img = imagecreatetruecolor($width, 16);
	$green = imagecolorallocate($img, ($width/0.8)*0.25, 255 - ($width/0.8)*0.25, 0);

	// Draw a white rectangle
	imagefilledrectangle($img, 0, 0, $width, 16, $green);
	
	// Capture the output buffer
	ob_start();

	// Dump the output buffer into a varibale instead of stdout
	imagegif($img);
	$img_buf = ob_get_contents();

	// Stop buffering and clean up
	ob_end_clean();
	imagedestroy($img);

	// Encode the raw image data in base64 for use with a data URI
	// Note: IE7 won't know what to do with this - for this lab, we won't care,
	// but workarounds do exist.
	$img_64 = base64_encode($img_buf);
	return '<img alt="Lead Scorer" src="data:image/png;base64,'.$img_64.'" />';
}


$query = "SELECT * FROM houses_users
			WHERE houses_users.house_id = " . $house_id
            ." AND houses_users.user_id = " . $_SESSION['id'];
$row = mysql_fetch_array(mysql_query($query));

if(ISSET($row['privilege']))
	$privilege = $row['privilege'];
else
	$privilege = 0;
?>


<?php include('templates/main/header.php'); ?>
	<div id="body">
	<div id="content" class="right_sidebar">
	<div id="houses">
		<hr>
		<h1>House Details</h1>
		<hr>
		<br />
		<br />

	<div id="map_canvas" class="map_canvas" ></div>

	<?php
		$query = "SELECT * FROM houses
				WHERE houses.id = " . $house_id;
		$row = mysql_fetch_array(mysql_query($query));
		$a_index = strpos($row['address'], ',' , 0);
		$address1 = substr($row['address'], 0, ($a_index));
		$address2 = substr($row['address'], $a_index+1, strlen($row['address']));
		echo '<h1 class="house-name">'.$row['name'].'</h1>';
		echo		'<p class="address" id="address1" >'.$address1.'</p>';
		echo		'<p class="address" id="address2" >'.$address2.'</p>';

	?>
	<hr class="hr-houses">
	<h2>Bills</h2>
	<table>
	<?php
		if($privilege == 0)
		{
			echo "YOU DO NOT BELONG TO THIS HOUSE!";
		}
		else
		{
			$query = "SELECT *, bills.id AS bill_id FROM bills
			JOIN categories ON categories.id = bills.category_id
			JOIN users ON users.id = bills.user_id
			WHERE bills.house_id = " . $house_id .
			" ORDER BY bills.create_time DESC";

			$results = mysql_query($query);
			while($row = mysql_fetch_array($results))
			{
				echo '<tr>';
				echo '<td>'.$row['create_time'].'</td><td>'.$row['firstname'].' '.$row['lastname'].'</td><td>paid</td><td class="amount">$'.$row['amount'].'</td><td>for</td><td>'.$row['notes'].'</td>';
				echo '</tr>';
			}
		}
	?>
	</table>

	<hr class="hr-houses">
	<h2>Members</h2>
	<?php
		$query = "SELECT * FROM houses_users
		JOIN users ON users.id = houses_users.user_id
		WHERE houses_users.house_id = " . $house_id;

		$results = mysql_query($query);
		while($row = mysql_fetch_array($results))
		{
			//print_r($row);
			echo '<div class="members">';
			echo '<p><img border="1px" src="uploads/';
			if (file_exists("uploads/" . md5($row['id'].$row['username']).'.png'))
			{
			  echo  md5($row['id'].$row['username']);
			}
			else
			{
			  echo 'zombie';
			}	
			echo '.png" width="120" height="120"></p>';
			echo '<br /><br /><br /><h3>'.$row['firstname'].'<br />'.$row['lastname'].'</h3><br />';
			echo '<p class="e-mail"><a href="mailto:'.$row['username'].'">'.$row['username'].'</a></p>';
			echo '</div>';
		}
	?>
	
	<hr class="hr-houses">
	<h2>Monthly Summary</h2>
	
	<?php
		$query = "SELECT SUM( amount ) AS amount, categories.name AS category FROM categories JOIN bills ON categories.id = bills.category_id WHERE bills.house_id =".$house_id." GROUP BY categories.id ORDER BY amount DESC";
	
		$results = mysql_query($query);
		while($row = mysql_fetch_array($results))
		{
			echo '<h3>'.$row['category'].'</h3>';
			echo		'Amount:&nbsp;&nbsp;&nbsp;'.create_image($row['amount']).' '. $row['amount'];
			echo '<br />';
		}
	?>
	
	<hr class="hr-houses">

	<?php
		if ($privilege == 1)
		{
			echo "<hr><h1><a href=\"invite.php?id=".$house_id."\" >Invite more members</a></h1><hr>";
		}
	?>
	</div>
	</div>
	<div id="right_sidebar">
		<p>
			<h2>My Houses</h2>
			<?php
				$query = "SELECT * FROM houses
							JOIN houses_users ON houses.id = houses_users.house_id
							WHERE houses_users.user_id = " . $_SESSION['id'];
				$results = mysql_query($query);
				while($row = mysql_fetch_array($results))
				{
					echo "<p><a href=\"houses.php?id=".$row['house_id']."\">".$row['name']."</a><p>";
				}
			?>
		</p>
	</div>
</div>

    <script>
    	var address = document.getElementById('address1').innerHTML + document.getElementById('address2').innerHTML;
		var geocoder;
		var map;

		geocoder = new google.maps.Geocoder();
		var latlng = new google.maps.LatLng(-34.397, 150.644);
		var mapOptions = 
		{
		  zoom: 18,
		  mapTypeId: 'satellite'
		}
		map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
      	
      	geocoder.geocode({ 'address': address},
          function(results, status)
          {
            if (status == google.maps.GeocoderStatus.OK)
            {
              map.setCenter(results[0].geometry.location);
            }
            else
            {
              alert('Geocode was not successful for the following reason: ' + status);
            }
          });

		 var contentString =
              '<h4>'+'Sharp 101'+'</h4>';//add pop-up window content
          var infowindow = new google.maps.InfoWindow({
              content: contentString//create pop-up window
          });

          map.setTilt(45);
    </script>

<?php include('templates/main/footer.php'); ?>




